package javaStu.producerAndConsumer;

import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TransferQueue;

/**
 * 生产消费模式
 * 用两个线程，一个输出字母，一个输出数字，交替输出1A2B3C4D5E...26Z
 *
 * @author: leiwenkang
 * @date: 2020/7/13
 * @time: 20:22
 */
public class ProducerAndConsumer_TransferQueue {
    public static void main(String[] args) {

        char[] ch1 = "1234567".toCharArray();
        char[] ch2 = "ABCDEFG".toCharArray();

        TransferQueue<Character> queue = new LinkedTransferQueue<>();
        new Thread(() -> {
            try {
                for (char c : ch1) {
                    System.out.print(queue.take());
                    queue.transfer(c);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "t1").start();


        new Thread(() -> {
            try {
                for (char c : ch2) {
                    queue.transfer(c);
                    System.out.println(queue.take());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "t2").start();

    }

}
